#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

int isyear(int y)
{
    return ((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0);
}

int main()
{
    int year, month, day, num;
    int k;
    cin >> k;
    int monthd[2][13] = {
        {0,31,28,31,30,31,30,31,31,30,31,30,31},
        {0,31,29,31,30,31,30,31,31,30,31,30,31}
    };

    while (k != 0)
    {
        cin >> year >> month >> day >> num;
        int d = day + num;
        int ret = isyear(year);
        while (d > monthd[ret][month])
        {
            d -= monthd[ret][month++];
            if (month > 12)
            {
                ++year;
                ret = isyear(year);
                month = 1;
            }
        }
        day = d;
        printf("%04d-%02d-%02d", year, month, day);
        cout << endl;
        --k;
    }
    return 0;
}


//bool isyear(int y)
//{
//    return ((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0);
//}
//
//int main()
//{
//    int monthday[2][13] = {
//        {0,31,28,31,30,31,30,31,31,30,31,30,31},
//        {0,31,29,31,30,31,30,31,31,30,31,30,31}
//    };
//    int year, month, day;
//    int num = 0;
//    month = 0;
//    while (scanf("%d%d", &year, &num) != EOF)
//    {
//        int is = isyear(year);
//        while (num > monthday[is][month])
//        {
//            num -= monthday[is][month];
//            ++month;
//        }
//        day = num;
//        printf("%04d-%02d-%02d", year, month, day);
//    }
//    return 0;
//}